import 'package:flutter/material.dart';
import 'package:pui_flutter/src/pui_flutter.dart';

class MultistateDemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyContentPage(),
    );
  }
}

class MyContentPage extends StatefulWidget with PPMultiStateMixin {
  MyContentPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MyContentPageState();
  }
}

class MyContentPageState extends State<MyContentPage> {
  var flag = 0;
  PPMultiState multiState = PPMultiState.LOADING;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("多状态布局Demo"),
        actions: [
          TextButton(
              onPressed: () {
                changeState();
              },
              child: const Text(
                "切换状态",
                style: TextStyle(color: Colors.white),
              ))
        ],
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Text(
              "当前状态($flag)：$multiState",
            ),
            PPMultiStateLayout(
              controller: widget,
              expanded: true,
              initState: PPMultiState.LOADING,
              body: const Text("我是内容布局"),
              emptyState: PPMultiStateConfig(
                title: '暂无关注的店铺，快去首页看看吧',
                isShowOperateView: true,
                operateText: "去逛逛",
              ),
              exceptionState: PPMultiStateConfig(title: '网络请求异常，请稍后刷新重试'),
              customState: PPMultiStateConfig.custom(
                title: '请先登录账号~',
                isShowOperateView: true,
                operateText: "上传尺码图 上传尺码图",
                buttonGradient: [
                  const Color(0xFF667EF8),
                  const Color(0xFF4057EC)
                ],
              ),
              onOperateFunc: (state) {
                debugPrint("$state");
              },
            ),
          ],
        ),
      ),
    );
  }

  void changeState() {
    flag += 1;
    if (flag > 6) {
      flag = 0;
    }
    switch (flag) {
      case 0:
        multiState = PPMultiState.LOADING;
        widget.changeState(PPMultiState.LOADING);
        break;
      case 1:
        multiState = PPMultiState.CONTENT;
        widget.changeState(PPMultiState.CONTENT);
        break;
      case 2:
        multiState = PPMultiState.EMPTY;
        widget.changeState(PPMultiState.EMPTY);
        break;
      case 3:
        multiState = PPMultiState.EXCEPTION;
        widget.changeState(PPMultiState.EXCEPTION);
        break;
      case 4:
        multiState = PPMultiState.EXCEPTION;
        widget.changeState(PPMultiState.EXCEPTION, title: "我是接口返回的错误");
        break;
      case 5:
        multiState = PPMultiState.CUSTOM;
        widget.changeState(PPMultiState.CUSTOM);
        break;
      case 6:
        widget.changeState(PPMultiState.EXCEPTION, title: "我是接口返回的自定义状态");
        break;
    }
    setState(() {});
  }
}
